import { Exclude, Expose, Transform, Type } from "class-transformer";
import { IsArray, IsNotEmpty, IsNumber, IsOptional, IsString, Length } from "class-validator";

export class PublicUserDto {
    @Expose()
    id: string | number;

    @Expose()
    username: string;

    @IsString()
    @IsNotEmpty()
    @Length(6, 20, {
        message: ({ constraints }) => `密码长度必须在${constraints[0]}到${constraints[1]}之间`,
    })
    @Exclude()
    password: string;

    // @IsArray()
    // @IsOptional()
    // /* @IsNumber({}, {
    //     each: true, // 每个数字都需要满足条件
    // }) */
    // // @Transform((value: any) => value.map((e: any) => parseInt(e))) // 将字符串数组转换为数字数组
    // roles: number[];


    @Transform(({ value }) => value.map((item: any) => ({
        name: item.Role.name,
        permissions: item.Role.permissions,
    })))
    @Expose({ name: 'UserRole' })
    roles: any;
}